package com.spic.system.activiti.utils;

import com.spic.system.api.domain.HiddenTodoTask;

import java.util.List;

/**
 * @author 290512
 * @create 2021-12-08 18:25
 */
public class PageUtils
{
    /**
     * 开始分页
     * @param list
     * @param pageNum 页码
     * @param pageSize 每页多少条数据
     * @return
     */
    public static List<HiddenTodoTask> startPage(List<HiddenTodoTask> list, Integer pageNum,
                                 Integer pageSize) {
        if (list == null) {
            return null;
        }
        if (list.size() == 0) {
            return null;
        }
        Integer count = list.size(); // 记录总数
        Integer pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }
        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引
        if (pageNum > pageCount){
            return null;
        }
        if (!pageNum.equals(pageCount)) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }
        List<HiddenTodoTask> pageList = list.subList(fromIndex, toIndex);

        return pageList;
    }
}
